package com.xing.controller;

import com.xing.DTO.GeneratorDTO;
import com.xing.Utils.Result;
import com.xing.service.TableListService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @Author Xing
 * @Date 2023/8/8 18:21
 * @version 1.0
 */
@RestController
@RequestMapping("generator")
@CrossOrigin
@Tag(name = "代码生成器")
public class GeneratorController {
    @Autowired
    private TableListService service;

    @Operation(summary = "获取数据库所有表")
    @GetMapping("/list")
    @PreAuthorize("hasAnyAuthority('admin')")
    public Result getTableList(@RequestParam Map<String,Object> params){
        return service.getTableList(params);
    }
    @Operation(summary = "获取数据库字段")
    @GetMapping("/{tableName}")
    public Result getTable(@PathVariable("tableName") String tableName){
        return service.getTable(tableName);
    }
    @Operation(summary = "代码生成器")
    @PostMapping("/generator")
    public Result generate(@RequestBody GeneratorDTO dto){
        try {
            service.generate(dto);
        }catch (Exception e){
            return new Result().err(110);
        }
        return new Result().ok();
    }

    @Operation(summary = "修改配置")
    @PutMapping("change")
    public Result change(@RequestBody GeneratorDTO dto){
        return service.change(dto);
    }

    @Operation(summary = "获取配置")
    @GetMapping("/setting")
    public Result getSetting() {
        return service.GetSetting();
    }
}
